﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web.Mvc;
using Microsoft.Practices.EnterpriseLibrary.Logging;

namespace TSharp.Core.AppBox.CodeBase
{
    public class CustomDataAnnotationsModelValidatorProvider : System.Web.Mvc.DataAnnotationsModelValidatorProvider
    {
        protected override IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context, IEnumerable<Attribute> attributes)
        {
            foreach (ValidationAttribute attribute in attributes.OfType<ValidationAttribute>())
            {
#if NET40
                if (!string.IsNullOrWhiteSpace(attribute.ErrorMessage))
#else
                if (!string.IsNullOrEmpty(attribute.ErrorMessage))
#endif
                {
                  //  attribute.ErrorMessage = attribute.ErrorMessage.Localize();
                    Logger.Writer.Warn(attribute.ErrorMessage);
                }
            }
            var validators = base.GetValidators(metadata, context, attributes);
            return validators;
        }

        protected override System.ComponentModel.ICustomTypeDescriptor GetTypeDescriptor(Type type)
        {
            return base.GetTypeDescriptor(type);
        }
    }
}